package com.childenglish.mapper;

import com.childenglish.entity.SysRole;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface SysRoleMapper {

    @Select("SELECT r.* FROM sys_role r " +
            "LEFT JOIN sys_user_role ur ON r.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND r.status = '0'")
    List<SysRole> selectRolesByUserId(Long userId);

    /**
     * 根据角色key查询角色ID
     */
    @Select("SELECT role_id FROM sys_role WHERE role_key = #{roleKey} AND status = '0'")
    Long findRoleIdByRoleKey(@Param("roleKey") String roleKey);

    /**
     * 插入用户角色关联
     */
    @Insert("INSERT INTO sys_user_role (user_id, role_id) VALUES (#{userId}, #{roleId})")
    int insertUserRole(@Param("userId") Long userId, @Param("roleId") Long roleId);

    /**
     * 删除用户角色关联
     */
    @Delete("DELETE FROM sys_user_role WHERE user_id = #{userId}")
    int deleteUserRoleByUserId(@Param("userId") Long userId);

    /**
     * 更新用户角色关联（先删除再插入）
     */
    @Update("UPDATE sys_user_role SET role_id = #{roleId} WHERE user_id = #{userId}")
    int updateUserRole(@Param("userId") Long userId, @Param("roleId") Long roleId);
}